import db.MYSQLControl;
import driver.PhantomJsDriver;
import driver.chromedriver;
import model.model;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import parse.priceParse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class price_old {
    public static void main(String[] args) throws InterruptedException, IOException {
        WebDriver driver = null;

        try {

            driver = chromedriver.getChromeDriver();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //driver = PhantomJsDriver.getPhantomJSDriver();
        driver.get("http://car.bitauto.com/tree_chexing/mb_9/");
        driver.manage().window().setSize(new Dimension(1000, 30000));
        driver.manage().window().maximize();
        //System.out.printf("now accesss %s \n", driver.getCurrentUrl());
        Thread.sleep(5000);
        List<WebElement> search_result = driver.findElements(By.xpath("//div[@class='brand-name']/span"));
        List<String> brand = new ArrayList();
        /*
        for(WebElement result : search_result){
            brand.add(result.getText());
        }
        String factory;
        String Car_big;
        List<String> brand1 = new ArrayList();
        for (String result : brand){
            if(!result.equals("GMC")){
                brand1.add(result);
            }else {
                break;
            }
        }
        for(String result :brand1){
            brand.remove(result);
        }*/
        String factory;
        String Car_big;
        brand.add("阿斯顿·马丁");




        for (String result : brand){
            (new WebDriverWait( driver, 10)) .until(
                    ExpectedConditions.presenceOfElementLocated(By.xpath(String.format("//span[text()='%s']",result)))
            ).click();
            List<WebElement> search_result1 = driver.findElements(By.xpath("//*[@id=\"divCsLevel_0\"]/h5"));

            if(search_result1.size()==0) {

                List<WebElement> search_result3 =  driver.findElements(By.xpath("//*[@id=\"divCsLevel_0\"]/div/div"));
                for(int j=1;j<search_result3.size()+1;j++){
                    String current_handles = driver. getWindowHandle();
                    // System.out.println(i+"aaa"+j+"mmm"+driver.findElement(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div[%d]/div[%d]/div/ul/li[1]",i,j))).getText());
                    List<WebElement> search_result4 =  driver.findElements(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div/div[%d]/div/ul/li",j)));
                    WebElement element = driver.findElement(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div/div[%d]/div/div/a/img",  j)));
                    Actions actions = new Actions(driver);
                    actions.moveToElement(element).click().perform();
                    Thread.sleep(1000);
                    Set<String> all_handles = driver.getWindowHandles();
                    Iterator<String> it = all_handles.iterator();
                    factory = result;
                    Car_big = driver.findElement(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div/div[%d]/div/ul/li[1]",j))).getText();
                    while(it.hasNext()){
                        if(current_handles == it.next()) continue;
//跳入新窗口
//
                        WebDriver window = driver.switchTo().window(it.next());
                        Thread.sleep(1000);
                        try{
                                WebElement element1 = driver.findElement(By.xpath("//*[contains(text(),\"停售年款\")]"));

                                Thread.sleep(1000);
                                actions.moveToElement(element1).click().perform();

                                List<WebElement> search_result_stop = driver.findElements(By.xpath("//*[contains(text(),\"停售年款\")]/../div/a"));
                                for(int i_stop = 0;i_stop<search_result_stop.size();i_stop++){
                                    util.writeMethod1(search_result_stop.get(i_stop).getAttribute("href"));
                                    System.out.println(search_result_stop.get(i_stop).getAttribute("href"));
                                    /*try {
                                        element1 = driver.findElement(By.xpath("//*[contains(text(),\"停售年款\")]"));
                                        actions.moveToElement(element1).click().perform();
                                        search_result_stop = driver.findElements(By.xpath("//*[contains(text(),\"停售年款\")]/../div/a"));
                                        actions.moveToElement(search_result_stop.get(i_stop)).click().perform();

                                        (new WebDriverWait( driver, 10)) .until(
                                                ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"car_tag\"]/nav/div/div/ul/li[2]/a"))
                                        ).click();
                                        Thread.sleep(2000);
                                        //System.out.println(driver.getCurrentUrl());
                                        List<model> data = priceParse.getData(factory,result,Car_big,driver.getPageSource());
                                        MYSQLControl.inserCarRecall(data);
                                    }catch (TimeoutException e){

                                    }*/


                                }
                        }catch (NoSuchElementException e) {

                        }

                        driver.close();
                    }
                    driver.switchTo().window(current_handles);


                    /*try {
                        qr.update(String.format("INSERT INTO car_price VALUES ('%s','%s','%s','%s');",result,search_result1.get(i-1).getText(),driver.findElement(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div[%d]/div[%d]/div/ul/li[1]",i,j))).getText(),driver.findElement(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div[%d]/div[%d]/div/ul/li[%d]",i,j,search_result4.size()))).getText()));
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    */
                }
                Thread.sleep(1000);

            }else {
                for (int i = 1; i < search_result1.size() + 1; i++) {
                    List<WebElement> search_result3 = driver.findElements(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div[%d]/div", i)));
                    for (int j = 1; j < search_result3.size() + 1; j++) {
                        String current_handles = driver.getWindowHandle();
                        // System.out.println(i+"aaa"+j+"mmm"+driver.findElement(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div[%d]/div[%d]/div/ul/li[1]",i,j))).getText());
                        List<WebElement> search_result4 = driver.findElements(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div[%d]/div[%d]/div/ul/li", i, j)));
                        WebElement element = driver.findElement(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div[%d]/div[%d]/div/div/a/img", i, j)));
                        Actions actions = new Actions(driver);
                        actions.moveToElement(element).click().perform();
                        Thread.sleep(1000);
                        Set<String> all_handles = driver.getWindowHandles();
                        Iterator<String> it = all_handles.iterator();
                        factory = search_result1.get(i - 1).getText();
                        Car_big = driver.findElement(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div[%d]/div[%d]/div/ul/li[1]", i, j))).getText();
                        while (it.hasNext()) {
                            if (current_handles == it.next()) continue;
//跳入新窗口
                            WebDriver window = driver.switchTo().window(it.next());
                            Thread.sleep(1000);

                            try{

                                    WebElement element1 = driver.findElement(By.xpath("//*[contains(text(),\"停售年款\")]"));

                                    Thread.sleep(1000);
                                    actions.moveToElement(element1).click().perform();

                                    List<WebElement> search_result_stop = driver.findElements(By.xpath("//*[contains(text(),\"停售年款\")]/../div/a"));
                                    for(int i_stop = 0;i_stop<search_result_stop.size();i_stop++){
                                        System.out.println(search_result_stop.get(i_stop).getAttribute("href"));

                                        util.writeMethod1(search_result_stop.get(i_stop).getAttribute("href"));
                                        /*try {
                                            element1 = driver.findElement(By.xpath("//*[contains(text(),\"停售年款\")]"));
                                            actions.moveToElement(element1).click().perform();
                                            search_result_stop = driver.findElements(By.xpath("//*[contains(text(),\"停售年款\")]/../div/a"));
                                            actions.moveToElement(search_result_stop.get(i_stop)).click().perform();

                                            (new WebDriverWait(driver, 10)).until(
                                                    ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"car_tag\"]/nav/div/div/ul/li[2]/a"))
                                            ).click();
                                            Thread.sleep(2000);
                                            //System.out.println(driver.getCurrentUrl());
                                            List<model> data = priceParse.getData(factory, result, Car_big, driver.getPageSource());
                                            MYSQLControl.inserCarRecall(data);
                                        }catch (TimeoutException e){

                                        }*/


                                    }
                            }catch (NoSuchElementException e) {

                            }
                            driver.close();
                        }
                        driver.switchTo().window(current_handles);


                    /*try {
                        qr.update(String.format("INSERT INTO car_price VALUES ('%s','%s','%s','%s');",result,search_result1.get(i-1).getText(),driver.findElement(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div[%d]/div[%d]/div/ul/li[1]",i,j))).getText(),driver.findElement(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div[%d]/div[%d]/div/ul/li[%d]",i,j,search_result4.size()))).getText()));
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    */
                    }
                    Thread.sleep(1000);
                }
                Thread.sleep(1000);
            }
        }
        driver.quit();

    }
}
